define([ 'game/consts' ], function(consts) {
    return {

        axial_to_center_xy: function(ac) {
            var size = consts.el;
            var result = {};
            result.x = size * Math.sqrt(3) * (ac.x + ac.y / 2);
            result.y = size * 3 / 2 * ac.y;
            return result;
        },

        corner_xy: function(center, index, ratio) {
            if (typeof ratio == 'undefined')
                ratio = 1;
            var size = consts.el;
            var result = {};
            var angle = 2 * Math.PI / 6 * (index + 0.5);
            result.x = center.x + ratio * size * Math.cos(angle);
            result.y = center.y + ratio * size * Math.sin(angle);
            return result;
        },

        axial_to_cube: function(ac) {
            return {
                x: ac.x,
                y: -ac.x - ac.y,
                z: ac.y
            };
        },

        cube_to_axial: function(cc) {
            return {
                x: cc.x,
                y: cc.z
            };
        },

        cube_round: function(cc) {
            var rx = Math.round(cc.x);
            var ry = Math.round(cc.y);
            var rz = Math.round(cc.z);
            var dx = Math.abs(rx - cc.x);
            var dy = Math.abs(ry - cc.y);
            var dz = Math.abs(rz - cc.z);
            if (dx > dy && dx > dz) {
                rx = -ry - rz;
            } else if (dy > dz) {
                ry = -rx - rz;
            } else {
                rz = -rx - ry;
            }
            return {
                x: rx,
                y: ry,
                z: rz
            };
        },

        xy_to_axial: function(x, y) {
            var size = consts.el;
            var q = (1 / 3 * Math.sqrt(3) * x - 1 / 3 * y) / size;
            var r = 2 / 3 * y / size;
            var cc = this.axial_to_cube({
                x: q,
                y: r
            });
            cc = this.cube_round(cc);
            return this.cube_to_axial(cc);
        },

        /**
         * Calculates distance between two points in regular geometry
         */
        dist_xy: function() {
            var args = new Array();
            Array.prototype.push.apply(args, arguments);
            var p1 = args.shift(), p2;
            if (typeof p1 == 'object') {
                p2 = args.shift();
            } else {
                p1 = {
                    x: p1,
                    y: args.shift()
                };
                p2 = {
                    x: args.shift(),
                    y: args.shift()
                };
            }
            return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
        }

    };
});
